package com.bidevalution.controller.api;

import com.bidevalution.business.context.LogType;
import com.bidevalution.business.service.LogService;
import com.bidevalution.dao.entity.LogInfo;
import com.qtp.core.model.Paging;
import com.qtp.core.model.Response;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.GsonUtil;
import com.qtp.core.util.IdsUtil;
import com.qtp.core.util.OConvertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Log4j2
@RestController
@Api(tags = "日志管理")
@RequestMapping("/log")
public class LogController {

    @Autowired
    LogService logService;

    @ApiOperation(value = "记录日志", notes = "插入日志信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "logType", value = "日志类别('SERVICE_API','EXTERNAL_REQUEST','ORDINARY')", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "logUrl", value = "记录日志时当前环境的链接", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "logParameter", value = "记录日志时当前环境的链接参数", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "logContent", value = "日志内容", dataType = "String", paramType = "query",required = true),
            @ApiImplicitParam(name = "logCreaterUserId", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "logCreaterTime", value = "创建时间(不传默认当前时间)格式yyyy-MM-dd HH:mm:ss", dataType = "String", paramType = "query")
    })
    @PostMapping("/insertLog")
    public Response insertLog(@RequestParam("logType") String logType,
                              @RequestParam("logContent") String logContent,
                              @RequestParam(name = "logUrl",required = false) String logUrl,
                              @RequestParam(name = "logParameter",required = false) String logParameter,
                              @RequestParam(name = "logCreaterUserId",required = false) String logCreaterUserId,
                              @RequestParam(name = "logCreaterTime",required = false) String logCreaterTime) {
        try {
            if (!LogType.exist(logType)){
                return Response.fail(String.format("logType（日志类别）的取值范围为%s",GsonUtil.toJson(LogType.values())));
            }
            LogInfo logInfo = new LogInfo();
            logInfo.setLogId(IdsUtil.uuid());
            logInfo.setLogType(logType);
            logInfo.setLogUrl(logUrl);
            logInfo.setLogParameter(logParameter);
            logInfo.setLogContent(logContent);
            logInfo.setLogCreaterUserId(logCreaterUserId);
            if (OConvertUtils.isEmpty(logCreaterTime)){
                logCreaterTime = DateUtil.getCurrentDateStr(DateUtil.PT2);
            }
            logInfo.setLogCreaterTime(logCreaterTime);
            if (logService.insertLogInfo(logInfo) == 1){
                return Response.ok();
            }else {
                return Response.fail("记录日志失败！");
            }
        }catch (Exception e){
            return Response.fail(GsonUtil.toJsonWithGson(e));
        }
    }

    @ApiOperation(value = "分页查询日志", notes = "分页查询日志信息", response = LogInfo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "logType", value = "日志类别('SERVICE_API','EXTERNAL_REQUEST','ORDINARY')", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "logCreaterUserId", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间：格式yyyy-MM-dd HH:mm:ss", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间：格式yyyy-MM-dd HH:mm:ss", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startIndex", value = "从第几条开始(默认0)", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "查询几条(默认100)", dataType = "int", paramType = "query",defaultValue = "100"),
            @ApiImplicitParam(name = "orderByTime", value = "根据创建时间排序1：正序2：倒序", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "orderByLogType", value = "根据日志类型排序1：正序2：倒序", dataType = "int", paramType = "query")
    })
    @PostMapping("/getLogInfoPaging")
    public Response<Paging<LogInfo>> getLogInfoPaging(@RequestParam(name = "logType",required = false) String logType,
                                                @RequestParam(name = "logCreaterUserId",required = false) String logCreaterUserId,
                                                @RequestParam(name = "startTime",required = false) String startTime,
                                                @RequestParam(name = "endTime",required = false) String endTime,
                                                @RequestParam(name = "startIndex",required = false,defaultValue = "0") int startIndex,
                                                @RequestParam(name = "pageSize",required = false,defaultValue = "100") int pageSize,
                                                @RequestParam(name = "orderByTime",required = false,defaultValue = "0") int orderByTime,
                                                @RequestParam(name = "orderByLogType",required = false,defaultValue = "0") int orderByLogType) {
        try {
            if (OConvertUtils.isNotEmpty(logType) && !LogType.exist(logType)){
                return Response.fail(String.format("logType（日志类别）的取值范围为%s",GsonUtil.toJson(LogType.values())));
            }
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("logType",logType);
            params.put("logCreaterUserId",logCreaterUserId);
            params.put("startTime",startTime);
            params.put("endTime",endTime);
            params.put("startIndex",startIndex);
            params.put("pageSize",pageSize);
            params.put("orderByTime",orderByTime);
            params.put("orderByLogType",orderByLogType);
            return Response.ok(logService.getLogInfoPaging(params));
        }catch (Exception e){
            return Response.fail(GsonUtil.toJsonWithGson(e));
        }
    }
}
